<?php
// get token
function get_token($uid, $appkey){
    $sql = "select id, uid, appkey, token, update_time from token where uid={$uid} and appkey={$appkey} and status=1;";
    $db = db_weibo();
    $re = $db->query($sql);
    $token = isset($re[0]['token']) ? $re[0]['token'] : ''; // error_code:21332 error:invalid_access_token
    return $token;
}




// add favorite
function add_favorite($mid, $uid, $favtime){
    if (empty($mid) || empty($uid) || empty($favtime)) return false;

    $addtime = date('Y-m-d H:i:s');
    $sql = "insert into favorite (mid, uid, favtime, addtime) values({$mid}, {$uid}, '{$favtime}', '{$addtime}');";
    $db = db_weibo();
    $re = $db->query($sql);
    return $re;
}

// get favorite
function get_favorite_by_uid($uid, $offset=0, $size=20){
    $sql = "select id, mid, uid, favtime, addtime from favorite where uid={$uid} order by favtime desc limit {$offset}, {$size};";
    $db = db_weibo();
    $re = $db->query($sql);
    return $re;
}

// check favorite exist
function favorite_exist($uid, $mid){
    $sql = "select id from favorite where uid={$uid} and mid={$mid} limit 1;";
    $db = db_weibo();
    $re = $db->query($sql);
    return $re;
}




// add user
function add_user($uid, $name, $gender, $ctime, $description='', $province=0, $city=0, $location='', $url='', $domain='', $weihao=''){
    if (empty($uid) || empty($name) || empty($gender) || empty($ctime)) return false;

    $addtime = date('Y-m-d H:i:s');
    $appkey = WB_AKEY;
    $sql = "insert into user (uid, name, gender, description, ctime, province, city, location, url, domain, weihao, addtime, appkey) values({$uid}, '{$name}', '{$gender}', '{$description}', '{$ctime}', {$province}, {$city}, '{$location}', '{$url}', '{$domain}', '{$weihao}', '{$addtime}', {$appkey});";
    $db = db_weibo();
    $re = $db->query($sql);
    return $re;
}

// check user exist
function user_exist($uid){
    $sql = "select id from user where uid={$uid} limit 1;";
    $db = db_weibo();
    $re = $db->query($sql);
    return $re;
}




// add mblog
function add_mblog($mid, $uid, $content, $ctime='', $small_pic='', $middle_pic='', $large_pic='', $source_url='', $source_name='', $raw_mid=0){
    if (empty($mid) || empty($uid) || empty($content) || empty($ctime)) return false;

    $addtime = date('Y-m-d H:i:s');
    $appkey = WB_AKEY;
    $sql = "insert into mblog (mid, uid, content, ctime, small_pic, middle_pic, large_pic, source_url, source_name, raw_mid, addtime, appkey) values({$mid}, {$uid}, '{$content}', '{$ctime}', '{$small_pic}', '{$middle_pic}', '{$large_pic}', '{$source_url}', '{$source_name}', {$raw_mid}, '{$addtime}', {$appkey});";
    $db = db_weibo();
    $re = $db->query($sql);
    return $re;
}

// check mblog exist
function mblog_exist($mid){
    $sql = "select id from mblog where mid={$mid} limit 1;";
    $db = db_weibo();
    $re = $db->query($sql);
    return $re;
}
